<template>
  <div>
    <el-table :header-cell-style="{ background: '#f5f6f6' }" :data="formPerms" border style="width: 100%">
      <el-table-column prop="title" show-overflow-tooltip label="表单字段">
        <template v-slot="scope">
          <span v-if="scope.row.required" style="color: #c75450"> * </span>
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="readOnly" label="只读" width="80">
        <template #header>
          <el-radio label="R" v-model="permSelect" @change="allSelect('R')">只读</el-radio>
        </template>
        <template v-slot="scope">
          <el-radio v-model="scope.row.perm" label="R" :name="scope.row.id"></el-radio>
        </template>
      </el-table-column>
      <el-table-column prop="editable" label="可编辑" width="90" v-if="nowNode.type !== 'CC'">
        <template #header>
          <el-radio label="E" v-model="permSelect" @change="allSelect('E')">可编辑</el-radio>
        </template>
        <template v-slot="scope">
          <el-radio v-model="scope.row.perm" label="E" :name="scope.row.id"></el-radio>
        </template>
      </el-table-column>
      <el-table-column prop="hide" label="隐藏" width="80">
        <template #header>
          <el-radio label="H" v-model="permSelect" @change="allSelect('H')">隐藏</el-radio>
        </template>
        <template v-slot="scope">
          <el-radio v-model="scope.row.perm" label="H" :name="scope.row.id"></el-radio>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { computed, onMounted, reactive, toRefs, watch } from 'vue'
import { useWorkFlowConfig } from '/@/stores/workFlow'
import { storeToRefs } from 'pinia'
import { deepCopy } from '/@/utils/utils'
export default {
    name: 'FormAuthorityConfig',
    setup() {
        const storesWorkFlowConfig = useWorkFlowConfig()
        const workFlowConfig = storeToRefs(storesWorkFlowConfig)
        const state = reactive({
            tableData: [],
            isIndeterminate: false,
            permSelect: '',
            checkStatus: {
                readOnly: true,
                editable: false,
                hide: false,
            },
        })
        const nowNode = computed(() => {
            return workFlowConfig.selectedNode.value
        })
        const formData = computed(() => {
            return workFlowConfig.design.value.formItems
        })
        const formPerms = computed(() => {
            return workFlowConfig.selectedNode.value.props.formPerms
        })
        const allSelect = (type) => {
            state.permSelect = type
            formPerms.value.forEach((f) => (f.perm = type))
        }
        const formPermsLoad = (oldPermMap, forms) => {
            forms.forEach((form) => {
                if (form.name === 'SpanLayout') {
                    formPermsLoad(oldPermMap, form.props.items)
                } else if (form.name === 'TableList') {
                    loadFormItemPerms(oldPermMap, form)
                    let cols = deepCopy(form.props.columns)
                    cols.forEach(item => item.title = `${form.title}·${item.title}`)
                    formPermsLoad(oldPermMap, cols)
                }else {
                    //刷新名称
                    loadFormItemPerms(oldPermMap, form)
                }
            })
        }
        const loadFormItemPerms = (oldPermMap, form) => {
            let old = oldPermMap.get(form.id)
            if (old) {
                old.title = form.title
                old.required = form.props.required
                formPerms.value.push(old)
            } else {
                formPerms.value.push({
                    id: form.id,
                    title: form.title,
                    required: form.props.required,
                    perm: workFlowConfig.selectedNode.value.type === 'ROOT' ? 'E' : 'R',
                })
            }
        }
        const handleCheckAllChange = () => {}

        watch(() => formPerms.value,
            () => {
                const set = new Set(formPerms.value.map((f) => f.perm))
                state.permSelect = set.size === 1 ? set.values()[0] : ''
            },
            {
                deep: true
            }
        )

        onMounted(() => {
             //备份
            let map = new Map()
            let oldPermMap = formPerms.value.forEach((v) => map.set(v[id], v))
            //重新清空，按顺序加载权限
            formPerms.value.length = 0
            formPermsLoad(oldPermMap, formData.value)
        })
        return {
            ...toRefs(state),
            nowNode,
            formData,
            formPerms,
            allSelect,
            formPermsLoad,
            loadFormItemPerms,
            handleCheckAllChange
        }
    }
}
</script>

<style lang="scss" scoped>
:deep(.el-table__row) {
  & > td:first-child {
    .cell {
      text-align: left;
    }
  }

  .cell {
    text-align: center;
  }

  .el-radio__label {
    display: none;
  }
}
</style>